//“给定一个price数组，它存储着每天的股票价格，第n个元素代表第n天的价格。如果最多只允”
//许进行1次买入和卖出，那么股票的最大收益是多少？



    function maxProfitFor1Time(prices) {
        if(prices==null || prices.length==0) {
            return 0;
        }
        let minPrice = prices[0];
        let maxProfit = 0;
        for (let i = 1; i < prices.length; i++) {
            let profit = prices[i] - minPrice;
            if(profit > maxProfit){
                maxProfit = profit;
            } else if (prices[i] < minPrice) {
                minPrice = prices[i];
            }
        }
        return maxProfit;
    }

    function maxProfitForAnyTime(prices) {
        let maxProfit = 0;
        for (let i = 1; i < prices.length; i++) {
            if (prices[i] > prices[i-1])
                maxProfit += prices[i] - prices[i-1];
        }
        return maxProfit;
    }


let prices = [9,2,7,4,3,1,8,4];
console.log(maxProfitFor1Time(prices));
console.log(maxProfitForAnyTime(prices));

